package com.wjw.java.collection.generic;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Generic_Exercise2 {
    public static void main(String[] args) {
        List<Employee> list = new ArrayList<>();
        list.add(new Employee("张三",5000.99,new MyBirthday(1991,10,3)));
        list.add(new Employee("李四",6000.99,new MyBirthday(1991,8,15)));
        list.add(new Employee("李四",6000.99,new MyBirthday(1986,8,26)));

        //按照员工的年龄,从大到小进行排序
        list.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee emp1, Employee emp2) {
                //防止传入的是子类
                if(!(emp1 instanceof Employee && emp2 instanceof Employee)){
                    return 0;
                }
                //调用Employee类中MyBirthday属性中重写的compareTo方法,用于比较年龄,从大到小排序
                return emp1.getBirthday().compareTo(emp2.getBirthday());
            }
        });
        System.out.println(list);
    }
}
//员工类
class Employee{
    private String name;
    private double salary;
    private MyBirthday birthday;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return this.salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public MyBirthday getBirthday() {
        return this.birthday;
    }

    public void setBirthday(MyBirthday birthday) {
        this.birthday = birthday;
    }

    public Employee(String name, double salary, MyBirthday birthday) {
        this.name = name;
        this.salary = salary;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "\n Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", birthday=" + birthday +
                '}';
    }
}
//生日类
class MyBirthday implements Comparable<MyBirthday>{

    private int year;
    private int month;
    private int day;

    public int getYear() {
        return this.year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return this.month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return this.day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public MyBirthday(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyBirthday{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public int compareTo(MyBirthday o) {
        //比较年是否相同
        int y = this.year - o.getYear();
        if(y != 0){//y == 0,年份相同
          return y;
        }
        //比较月是否相同
        int m = this.month - o.getMonth();
        if(m != 0){//m == 0,月份相同
            return m;
        }
        //比较日是否相同
        return this.day - o.getDay();
    }

}
